package org.sloth.core.load.proxy
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.media.Sound;
	import flash.net.URLRequest;
	
	import org.sloth.core.load.LoadVO;
	
	internal class LoadSound extends LoadAbstract
	{
//============================================================================
//PROPERTY
//============================================================================
		protected var _sound:Sound;
//============================================================================
//CONSTRUCTOR
//============================================================================
		public function LoadSound()
		{
			super();
		}
//============================================================================
//OVERRIDE
//============================================================================
		override public function load(vo:LoadVO):void
		{
			if (_isLoading) return;
			_isLoading = true;
			this.vo = vo;
			_sound = new Sound();
			_sound.addEventListener(Event.COMPLETE, complete_handler);
			_sound.addEventListener(IOErrorEvent.IO_ERROR, error_handler);
			_sound.addEventListener(ProgressEvent.PROGRESS, progress_handler);
			_sound.load(new URLRequest(vo.url));
		}
//============================================================================
//HANDLER
//============================================================================
		protected function complete_handler(event:Event):void
		{
			_isLoading = false;
			data = _sound;
			_sound = null;
			dispatchEvent(new Event("complete"));
		}
		protected function error_handler(event:IOErrorEvent):void
		{
			_isLoading = false;
			data = event.text;
			_sound = null;
			dispatchEvent(new Event("error"));
		}
		protected function progress_handler(event:ProgressEvent):void
		{
			var ratio:Number = event.bytesLoaded / event.bytesTotal;
			data = int(ratio * 10000) / 100;
			dispatchEvent(new Event("progress"));
		}
	}
}